package com.luoying.itew.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.luoying.itew.common.AntaiApplication;
import com.luoying.itew.common.SharedConfig;

public class SharedUtils {
    private static SharedPreferences shared;

    /**
     * 获取偏好文件对象
     *
     * @return
     */
    public static SharedPreferences getShared() {
        if (shared == null) {
            shared = AntaiApplication.context.getSharedPreferences(SharedConfig.SHARED_FILE_NAME, Context.MODE_PRIVATE);
        }
        return shared;
    }

    /**
     * 是否记住帐号
     *
     * @return
     */
    public static boolean isKeepAccount() {
        return getShared().getBoolean(SharedConfig.SHARED_IS_KEEP_ACCOUNT, false);
    }

    /**
     * 是否记住密码
     *
     * @return
     */
    public static boolean isKeepPwd() {
        return getShared().getBoolean(SharedConfig.SHARED_IS_KEEP_PWD, false);
    }

    /**
     * 获取用户帐号
     * @return
     */
    public static String getUserAccount(){
        return getShared().getString(SharedConfig.SHARED_USER_ACCOUNT, "");
    }

    /**
     * 获取用户密码
     * @return
     */
    public static String getUserPwd(){
        return getShared().getString(SharedConfig.SHARED_USER_PWD, "");
    }

    /**
     * 获取用户token
     * @return
     */
    public static String getUserToken(){
        return getShared().getString(SharedConfig.SHARED_USER_TOKEN, "");
    }

    /**
     * 保存用户token
     * @param userToken
     */
    public static void saveUserToken(String userToken){
        saveValus(SharedConfig.SHARED_USER_TOKEN,userToken);
    }

    /**
     * 获取鉴权加密值
     * @return
     */
    public static String getUserSign(){
        return getShared().getString(SharedConfig.SHARED_USER_SIGN, "");
    }

    /**
     * 保存值
     * @param key
     * @param value
     */
    private static void saveValus(String key,Object value){
        if(value==null){
            return;
        }
        SharedPreferences.Editor editor = getShared().edit();
        if(value instanceof String ){
            editor.putString(key, String.valueOf(value));
        }
        if(value instanceof Integer ){
            editor.putInt(key, (Integer)value);
        }
        if(value instanceof Boolean ){
            editor.putBoolean(key, (Boolean) value);
        }
        if(value instanceof Long ){
            editor.putLong(key, (Long) value);
        }
        editor.apply();
    }
}
